<!--客户表单-->
<template>
  <div class="contain">
    <div class="form-wrap mb30">
      <!-- <div class="form-wrap-row">
        <div class="line cf" @click="toDet">
          <div class="label f-l">设置角色和权限</div>
          <i class="iconfont icon-more f-r"></i>
        </div>
      </div> -->
      <!-- 选择以后的显示start -->
      <div class="form-choice">
        <popup-picker class="vux-form-require" title="设置角色" :data="roleType.list" :columns="3" v-model="roleType.value" placeholder="请选择" :show-name="roleType.show"></popup-picker>
      </div>
        <!-- <div class="line displayflex" @click="toDet">
          <div class="label">角色分配</div>
          <span class="flex1 txt-rit gray">业务员</span>
          <i class="iconfont icon-more"></i>
        </div> -->
      <!-- <div class="privi-detail displayflex">
        <span class="label">开通权限</span>
        <ul class="list flex1 cf">
          <li>审核</li>
          <li>通知配送</li>
        </ul>
      </div> -->
      <!-- 选择以后的显示end -->
    </div>

    <div class="form-wrap mt30">
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">
            <span class="red">*</span>姓名</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="name" /></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">
            <span class="red">*</span>手机号</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="tel" /></div>
        </div>
      </div>
      <div class="form-wrap-row" v-if="!staffid">
        <div class="line displayflex">
          <div class="label">
            <span class="red">*</span>密码</div>
          <div class="ipt flex1"><input type="password" placeholder="点击输入" v-model="pwd" /></div>
        </div>
      </div>
      <div class="form-wrap-row" v-if="!staffid">
        <div class="line displayflex">
          <div class="label">
            <span class="red">*</span>确认密码</div>
          <div class="ipt flex1"><input type="password" placeholder="点击输入" v-model="conpwd" /></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label">公司部门</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="depa" /></div>
        </div>
      </div>
      <div class="form-choice">
        <popup-picker title="性别" :data="sexType.list" :columns="3" v-model="sexType.value" placeholder="请选择" :show-name="sexType.show"></popup-picker>
      </div>
    </div>
    <!-- <div class="privi-wrap mt30">
      <p class="tit">角色权限（点击勾选）</p>
      <div class="privi-label">
        <label class="label">
          <input type="checkbox">
          <span class="txt">审核</span>
        </label>
        <label class="label">
          <input type="checkbox">
          <span class="txt">通知配送</span>
        </label>
        <label class="label">
          <input type="checkbox">
          <span class="txt">预约发货</span>
        </label>
        <label class="label">
          <input type="checkbox">
          <span class="txt">预约调度</span>
        </label>
      </div>
    </div> -->
    
    <div class="btn-save mt30" @click="reguster">完成</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      tel: "",
      pwd: "",
      conpwd: "",
      roleType: {
        list: [
          {
            value: "Salesman",
            name: "业务员"
          },
          // {
          //   value: "Hospital",
          //   name: "医院"
          // },
          // {
          //   value: "Dispatch",
          //   name: "运输调度"
          // },
          {
            value: "Warehouse",
            name: "仓管"
          },
          {
            value: "BillingClerk",
            name: "开票员"
          },
          {
            value: "ServiceManager",
            name: "业务经理"
          },
          {
            value: "Driver",
            name: "配送员"
          }
        ],
        value: [],
        show: true
      },
      depa:'',
      classfiy: '1',
      sexType:{
        list: [{
            value: "1",
            name: "男"
          },
          {
            value: "2",
            name: "女"
          }],
        value: [],
        show: true
      },
      staffid:'',
      PreRoleName:'',
    };
  },
  created() {
    this.staffid = this.$route.query.StaffID
    if(this.staffid){
      this.getData();  
    };
  },
  mounted() {
    this.$nextTick(function() {

    });
  },
  methods: {
    toDet() {
      this.$router.push({
        path: "/mail/set"
      });
    },
    getData(){
      var self = this;
      var userinfo = JSON.parse(localStorage.userinfo);
      var query = {
        TenantGUID: userinfo.TenantGUID,
        CorpGuid: userinfo.CorpGuid,
        StaffID: this.staffid
      };
      this.Ajax.post("SearchMfstaffbacies", query).then(function(resp) {
        if (resp.IsSuccess) {
          var msg = resp.Datas[0];
          self.name = msg.Name;
          self.tel = msg.MobileNo;
          self.pwd = msg.Pwd;
          self.conpwd = msg.Pwd;
          self.depa = msg.DeptName;
          self.sexType.value.push(msg.Sex);
          self.roleType.value.push(msg.UserBizType);
          self.PreRoleName = msg.UserBizType;
        } else {
          self.Toast({
            message: resp.ErrorMessage || "操作失败，请稍后重试"
          });
        }
      });
    },
    reguster() {
      if (!this.roleType.value[0] || this.roleType.value[0] == undefined) {
        this.Toast("请选择角色！");
        return false;
      };
      if (this.name === "") {
        this.Toast("请输入您的称呼！");
        return false;
      };
      var regTel = /^1[345789]\d{9}$/;
      if (this.tel == "" || !regTel.test(this.tel)) {
        this.Toast("请输入您的正确联系方式！");
        return false;
      };
      if(!this.staffid){
        if (this.pwd == "") {
          this.Toast("请输入密码！");
          return false;
        };
        if (this.pwd.length < 6) {
          this.Toast("密码长度至少6位！");
          return false;
        };
        if (this.conpwd !== this.pwd) {
          this.Toast("两次密码请填写一样！");
          return false;
        };
      };
      var userinfo = JSON.parse(localStorage.userinfo);
      var self = this;
      var methods = '';
      var query = {
                  Contacts:this.name,
                  ContactTel: this.tel,
                  CropName: userinfo.CorpName,
                  UserType: this.classfiy,
                  DeptName: this.depa,
                  Sex:this.sexType.value[0]
                };
      if(this.staffid){
        query.StaffID=this.staffid;
        query.PreRoleName=this.PreRoleName;
        query.CorpGuid=userinfo.CorpGuid;
        query.TenantGUID=userinfo.TenantGUID;
        query.RoleName=this.roleType.value[0];
        methods = 'UpdateUser';
      }else{
        query.Pwd = this.pwd;
        query.UserBizType = this.roleType.value[0];
        methods = 'RegisterUser';
      };
      // if(this.staffid){
      //   var query = {
      //     StaffID:this.staffid,
      //     Contacts:this.name,
      //     ContactTel: this.tel,
      //     CorpName: userinfo.CorpName,
      //     UserType: this.classfiy,
      //     DeptName: this.depa,
      //     Sex:this.sexType.value[0],
      //     PreRoleName: this.PreRoleName,
      //     CorpGuid: userinfo.CorpGuid,
      //     TenantGUID: userinfo.TenantGUID,
      //     RoleName: this.roleType.value[0],
      //   };
      //   methods = 'UpdateUser';
      // }else{
      //   var query = {
      //     Contacts: this.name,
      //     ContactTel: this.tel,
      //     CropName: userinfo.CorpName,
      //     Pwd: this.pwd,
      //     UserBizType: this.roleType.value[0],
      //     UserType: this.classfiy,
      //     DeptName:this.depa,
      //     Sex:this.sexType.value[0]
      //   };
      //   methods = 'RegisterUser';
      // };
      var _this = this;
      console.log(query)
      this.Ajax.post(methods , query).then(function(resp) {
        if (resp.IsSuccess) {
          _this.MessageBox.alert("保存成功").then(action => {
            _this.$router.push({
              path: "/mail"
            });
          });
        } else {
          _this.Toast({
            message: resp.ErrorMessage || "注册失败"
          });
        }
      });
    }
  }
};
</script>